<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.department" :placeholder="'面试状态'" style="width: 140px" class="filter-item">
        <el-option :label="'sw'" :value="1"></el-option>
        <el-option :label="'sw'" :value="1"></el-option>
      </el-select>
      <el-select v-model="listQuery.department" :placeholder="'开放状态'" style="width: 140px" class="filter-item">
        <el-option :label="'sws'" :value="1"></el-option>
        <el-option :label="'sws'" :value="1"></el-option>
      </el-select>
      <el-date-picker
        v-model="listQuery.date"
        :picker-options="pickerOptions"
        class="filter-item"
        style="width: 400px;line-height: 30px"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="有效开始时间"
        end-placeholder="有效结束时间"
      ></el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <!--<el-table-column :label="'序号'" fixed prop="id" align="center" width="65">-->
      <!--<template slot-scope="scope">-->
      <!--<span>{{ scope.row.id }}</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column :label="'姓名'" fixed width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'性别'" width="110px" align="center" prop="reviewer">
        <template slot-scope="scope">
          <span>{{ scope.row.sex_name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'推荐岗位'" width="110px" align="center" prop="author">
        <template slot-scope="scope">
          <span>{{ scope.row.recommend }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="'面试批次'" width="110px" align="center" prop="reviewer">
        <template slot-scope="scope">
          <span>{{ scope.row.batch }}</span>
        </template>
      </el-table-column>
      <el-table-column label="一面评价纬度">
        <el-table-column :label="'工作动力(20)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>10</span>
          </template>
        </el-table-column>
        <el-table-column :label="'专业技能(20)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>10</span>
          </template>
        </el-table-column>
        <el-table-column :label="'项目经验(20)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>10</span>
          </template>
        </el-table-column>
        <el-table-column :label="'学习创新(20)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>10</span>
          </template>
        </el-table-column>
        <el-table-column :label="'执行落实(20)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>10</span>
          </template>
        </el-table-column>
        <el-table-column :label="'总分(100)'" width="110px" align="center">
          <template slot-scope="scope">
            <span>100</span>
          </template>
        </el-table-column>
        <el-table-column :label="'其他评语'" width="110px" align="center" prop="reviewer">
          <template slot-scope="scope">
            <span></span>
          </template>
        </el-table-column>
        <el-table-column :label="'面试结论'" width="110px" align="center" prop="reviewer">
          <template slot-scope="scope">
            <span></span>
          </template>
        </el-table-column>
        <el-table-column :label="'面试官'" width="110px" align="center" prop="reviewer">
        </el-table-column>
        <el-table-column :label="'状态'" width="80px">
        </el-table-column>
      </el-table-column>
      <el-table-column :label="'操作'" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editTable(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>
    <el-dialog :visible.sync="dialogFormVisible" title="一面综合评价">
      <el-row>
        <el-col :span="6">
          <div class="z-center">
            <el-button type="info" plain>姓名：</el-button>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="z-center">
            <el-button type="info" plain>性别： 男</el-button>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="z-center">
            <el-button type="info" plain>推荐岗位： 的的电话</el-button>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="z-center">
            <el-button type="info" plain>面试批次： 2838388</el-button>
          </div>
        </el-col>
      </el-row>
      <div style="height: 20px;"></div>
      <el-alert :closable="false" title="一面评价维度" type="info" center>
      </el-alert>
      <div style="height: 25px;"></div>
      <div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">工作动力(20) </h3><el-input-number :min="1" :max="20" v-model="num1" @change="handleChange"></el-input-number>
        </div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">专业技能(20) </h3><el-input-number :min="1" :max="20" v-model="num1" @change="handleChange"></el-input-number>
        </div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">项目经验(20) </h3><el-input-number :min="1" :max="20" v-model="num1" @change="handleChange"></el-input-number>
        </div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">学习创新(20) </h3><el-input-number :min="1" :max="20" v-model="num1" @change="handleChange"></el-input-number>
        </div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">执行落实(20) </h3><el-input-number :min="1" :max="20" v-model="num1" @change="handleChange"></el-input-number>
        </div>
        <div style="margin: 5px 0 5px 0;">
          <h3 style="display: inline">总分(100) </h3><el-button>100</el-button>
        </div>
      </div>
      <div style="height: 20px;"></div>
      <el-alert :closable="false" title="其他评语" type="info" center>
      </el-alert>
      <div style="height: 25px;"></div>
      <div>
        <el-input :rows="6" type="textarea" size="medium"></el-input>
      </div>
      <div style="margin-top: 10px">
        <div style="display: inline-block;font-size: 16px;">面试状态 </div>
        <el-select v-model="num1" placeholder="请选择">
          <el-option :label="'一面通过'" :value="1"></el-option>
          <el-option :label="'一面未通过'" :value="1"></el-option>
          <el-option :label="'一面缺席'" :value="1"></el-option>
        </el-select>
      </div>
      <div style="margin-top: 10px;width: 50%;">
        <el-input placeholder="面试官"></el-input>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel"/>
        <el-table-column prop="pv" label="Pv"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchPv, createArticle, updateArticle } from '@/api/article'
import { getFirstList } from '@/api/interview'
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj ,such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'FirstInterview',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      num1: 1,
      num2: 1,
      num3: 1,
      num4: 1,
      num5: 1,
      totalCount: 0,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id',
        department: undefined,
        date: undefined
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      sortOptions: [
        { label: 'ID Ascending', key: '+id' },
        { label: 'ID Descending', key: '-id' }
      ],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [
          { required: true, message: 'type is required', trigger: 'change' }
        ],
        timestamp: [
          {
            type: 'date',
            required: true,
            message: 'timestamp is required',
            trigger: 'change'
          }
        ],
        title: [
          { required: true, message: 'title is required', trigger: 'blur' }
        ]
      },
      downloadLoading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChange() {
      this.totalCount = this.num1 + this.num2 + this.num3 + this.num4 + this.num5
    },
    editTable(id) {
      this.dialogFormVisible = true
      this.temp.title = id
    },
    getList() {
      this.listLoading = true
      getFirstList(this.listQuery.page).then(response => {
        this.list = response.data.data
        this.total = response.data.meta.pagination.total
        this.listQuery.page = response.data.meta.pagination.current_page++
      }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          this.temp.author = 'vue-element-admin'
          createArticle(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          updateArticle(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = [
          'timestamp',
          'title',
          'type',
          'importance',
          'status'
        ]
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === 'timestamp') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        })
      )
    }
  }
}
</script>

<style scoped>
  .z-center {
    text-align: left;
  }
</style>
